package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsPermissionService;
import com.qf.service.DtsRoleService;
import com.qf.util.Permission;
import com.qf.util.PermissionUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.PermVo;
import net.bytebuddy.implementation.bind.annotation.Pipe;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author wanghaotian
 * time:2022/12/17,0017 9:33
 * description:
 */

@RestController
@CrossOrigin("*")
@RequestMapping("/admin/role")
public class AdminRoleController {

    @Autowired
    private DtsRoleService roleService;

    @Autowired
    private DtsPermissionService permissionService;

    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("/options")
    public Object options(){
        List<DtsRole> roles = roleService.findAll();
        List<Map<String,Object>> options = new ArrayList<>();
        for (DtsRole role : roles) {
            Map<String,Object> map = new HashMap<>();
            map.put("value", role.getId());
            map.put("label", role.getName());
            options.add(map);
        }
        return ResponseUtil.ok(options);
    }

    @RequiresPermissions("admin:role:list")
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "查询")
    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page" ,defaultValue = "1",required = false) Integer page,
            @RequestParam(value = "limit" ,defaultValue = "20",required = false) Integer limit,
            String name,String sort,String order
    ){
        IPage<DtsRole> iPage = roleService.findPage(page,limit,name,sort,order);
        Map<String,Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    @RequiresPermissions("admin:role:create")
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsRole role){
        try {
            roleService.add(role);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseUtil.fail(-1, e.getMessage());
        }
        return ResponseUtil.ok(role);
    }

    @RequiresPermissions("admin:role:delete")
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsRole role){
        try {
            roleService.delete(role);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return ResponseUtil.fail(-1, e.getMessage());
        }
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:role:update")
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsRole role){
        roleService.update(role);
        return ResponseUtil.ok();
    }

    @PostMapping("/permissions")
    public Object permissions(@RequestBody Map<String,Object> map){
        Integer roleId = (Integer) map.get("roleId");
        List<String> permissions = (List<String>) map.get("permissions");
        if(roleService.isSuperAdmin(roleId)){
            return ResponseUtil.ok();
        }
        permissionService.updatePermissions(roleId,permissions);
        return ResponseUtil.ok();
    }

    @GetMapping("/permissions")
    public Object permissions(Integer roleId){
        List<Permission> permissions = PermissionUtil.listPermission(applicationContext, "com.qf.controller");
        List<PermVo> permVos = PermissionUtil.listPermVo(permissions);
        Set<String> nowPermissoins;
        if (roleService.isSuperAdmin(roleId)) {
            nowPermissoins = PermissionUtil.listPermissionString(permissions);
        }else {
            nowPermissoins = roleService.selectPermissions(roleId);
        }
        Map<String,Object> data = new HashMap<>();
        data.put("systemPermissions",permVos);
        data.put("assignedPermissions",nowPermissoins);
        return ResponseUtil.ok(data);
    }
}
